package cn.sytton.taffe.web.sys.controller;

import cn.sytton.taffe.web.sys.entity.UserEntity;
import cn.sytton.taffe.web.sys.service.IUserService;
import com.github.pagehelper.PageInfo;
import cn.sytton.taffe.excel.annotation.ExportExcel;
import cn.sytton.taffe.framework.base.controller.BaseController;
import cn.sytton.taffe.framework.base.entity.PageParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 *
 * @author skyrock
 */
@Tag(name = "用户")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user")
public class UserController extends BaseController {

    private final IUserService userService;

    @Operation(summary = "查询分页用户列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping
    public PageInfo<UserEntity> findPage(PageParam pp) {
        return userService.findListPageOfXml(pp);
    }

    @Operation(summary = "查询单个用户")
    @Parameter(name = "id", description = "用户ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public UserEntity findById(@PathVariable String id) {
        return userService.findById(id);
    }

    @Operation(summary = "查询多个用户", description = "body: 主键ID数组")
    @PostMapping("/ids")
    public List<UserEntity> findByIds(@RequestBody String[] ids) {
        return userService.findByIds(ids);
    }

    @Operation(summary = "保存用户")
    @PostMapping
    public void save(@RequestBody @Validated UserEntity entity) {
        userService.createOrUpdate(entity);
    }

    @Operation(summary = "删除用户", description = "body: 主键ID数组")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        userService.delete(ids);
    }

    @Operation(summary = "修改用户密码")
    @PostMapping("/pwd/update")
    public void updatePwd(@RequestBody UserEntity entity) {
        userService.updatePwd(entity);
    }

    @Operation(summary = "修改用户状态")
    @PostMapping("/status/update")
    public void updateUserStatus(@RequestBody UserEntity entity) {
        userService.updateUserStatus(entity);
    }

    @Operation(summary = "修改我的密码", description = "body: Map对象, {oldPwd: 旧密码, newPwd: 新密码}")
    @PostMapping("/my-pwd/update")
    public void updateMyPwd(@RequestBody Map<String, String> pwdData) {
        userService.updateMyPwd(pwdData.get("oldPwd"), pwdData.get("newPwd"));
    }
}
